pacman::p_load("cowplot","dismo","leaflet","maps","mapdata","OpenStreetMap","raster","rasterVis","readxl","rgbif","rgdal","tidyverse","utils", "rvertnet", "rinat")
## Installing package into 'C:/Users/Conno/OneDrive/Documents/R/win-library/4.1'
## (as 'lib' is unspecified)
## Warning: unable to access index for repository http://www.stats.ox.ac.uk/pub/RWin/bin/windows/contrib/4.1:
##   cannot open URL 'http://www.stats.ox.ac.uk/pub/RWin/bin/windows/contrib/4.1/PACKAGES'
## package 'OpenStreetMap' successfully unpacked and MD5 sums checked
## 
## The downloaded binary packages are in
##  C:\Users\Conno\AppData\Local\Temp\Rtmp2HlKK9\downloaded_packages
## 
## OpenStreetMap installed
## Warning in pacman::p_load("cowplot", "dismo", "leaflet", "maps", "mapdata", : Failed to install/load:
## OpenStreetMap
library(mgsub)
library(lubridate)
## 
## Attaching package: 'lubridate'
## The following objects are masked from 'package:raster':
## 
##     intersect, union
## The following object is masked from 'package:cowplot':
## 
##     stamp
## The following objects are masked from 'package:base':
## 
##     date, intersect, setdiff, union

state <- map_data("state")
county <- map_data("county")

tn <- county %>% 
  filter(region=="tennessee")

x <- tn[1]
y <- tn[2]

tn_SP <- FRK::df_to_SpatialPolygons(tn, "subregion", c("long", "lat"), proj =  CRS("+init=epsg:4269"))
tn_SP <- spTransform(tn_SP, "+proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs")
APSU_Marbled <- read.csv("https://raw.githubusercontent.com/CJHughes926/Herpmapping_Map_Assignment2/main/APSU_Ambystoma_opacum_TN.csv")
  
Vertnet_Marbled <- rvertnet::searchbyterm(scientificname = "Ambystoma opacum", stateprovince = "Tennessee", limit = 10000)
## Processing request...
## 
## Last Query URL: "http://api.vertnet-portal.appspot.com/api/search?q={"q":"scientificname:Ambystoma opacum stateprovince:Tennessee","l":10000}"
## 
## Matching records: 117 returned, 117 available
Vertnet_Marbled <- Vertnet_Marbled$data

Inat_Marbled <- rinat::get_inat_obs(taxon_name = "Ambystoma opacum", place_id = 45, maxresults = 1000, quality = "research", geo =  TRUE)

GBif_Marbled <- rgbif::occ_data(scientificName = "Ambystoma opacum",
                 hasCoordinate = TRUE, limit = 5000,
                 decimalLongitude = "-91, -81", 
                 decimalLatitude = "34, 37", )

GBif_Marbled <- GBif_Marbled$data
APSU_Marbled <- as.data.frame(APSU_Marbled)
APSU_Marbled <- subset(APSU_Marbled, is.na(APSU_Marbled$DEC_LAT) == FALSE)
APSU_Marbled <- subset(APSU_Marbled, APSU_Marbled$STATE_PROV == "Tennessee")
APSU_Marbled$COUNTY <- mgsub(APSU_Marbled$COUNTY, " County", "")

Vertnet_Marbled <- as.data.frame(Vertnet_Marbled)
Vertnet_Marbled <- subset(Vertnet_Marbled, is.na(Vertnet_Marbled$decimallatitude) == FALSE)
Vertnet_Marbled <- subset(Vertnet_Marbled, is.na(Vertnet_Marbled$decimallongitude) == FALSE)
Vertnet_Marbled$county <- mgsub(Vertnet_Marbled$county, " County", "")


Vertnet_Marbled$decimallatitude <- as.numeric(Vertnet_Marbled$decimallatitude)
Vertnet_Marbled$decimallongitude <- as.numeric(Vertnet_Marbled$decimallongitude)

Inat_Marbled <- as.data.frame(Inat_Marbled, )
Inat_Marbled <- subset(Inat_Marbled, Inat_Marbled$coordinates_obscured == "false")
Inat_Marbled <- subset(Inat_Marbled, is.na(Inat_Marbled$latitude) == FALSE)

GBif_Marbled <- as.data.frame(GBif_Marbled)
GBif_Marbled <- subset(GBif_Marbled, is.na(GBif_Marbled$decimalLatitude) == FALSE)
GBif_Marbled <- subset(GBif_Marbled, GBif_Marbled$stateProvince == "tennessee" | GBif_Marbled$stateProvince == "Tennessee" | GBif_Marbled$stateProvince == "TN" | GBif_Marbled$stateProvince == "tn")
County_Counts <- as.data.frame(toupper(unique(tn$subregion)))
names(County_Counts)[1] <- "County"
Vertnet_Counts <- mutate(as.data.frame(table(Vertnet_Marbled$county)))
APSU_Counts <- mutate(as.data.frame(table(APSU_Marbled$COUNTY)))
Vertnet_Counts$Var1 <- toupper(Vertnet_Counts$Var1)
names(Vertnet_Counts)[1] <- "County"
APSU_Counts$Var1 <- toupper(APSU_Counts$Var1)
names(APSU_Counts)[1] <- "County"

County_Counts$Total <- 0
County_Counts <- merge(County_Counts, APSU_Counts, all.x = TRUE)
names(County_Counts)[names(County_Counts) == 'Freq'] <- "Vertnet_Counts"
County_Counts <- merge(County_Counts, Vertnet_Counts, all.x = TRUE)
names(County_Counts)[names(County_Counts) == 'Freq'] <- "APSU_Counts"
County_Counts <- County_Counts %>% replace_na(list(Vertnet_Counts = 0, APSU_Counts = 0))

County_Counts$Total <- County_Counts$Vertnet_Counts + County_Counts$APSU_Counts

County_Counts
##        County Total Vertnet_Counts APSU_Counts
## 1    ANDERSON     0              0           0
## 2     BEDFORD     0              0           0
## 3      BENTON     1              0           1
## 4     BLEDSOE     0              0           0
## 5      BLOUNT     7              0           7
## 6     BRADLEY     1              0           1
## 7    CAMPBELL     0              0           0
## 8      CANNON     0              0           0
## 9     CARROLL     0              0           0
## 10     CARTER     0              0           0
## 11   CHEATHAM     3              3           0
## 12    CHESTER     0              0           0
## 13  CLAIBORNE     1              0           1
## 14       CLAY     0              0           0
## 15      COCKE     0              0           0
## 16     COFFEE     0              0           0
## 17   CROCKETT     0              0           0
## 18 CUMBERLAND     0              0           0
## 19   DAVIDSON     0              0           0
## 20    DE KALB     0              0           0
## 21    DECATUR     3              0           3
## 22    DICKSON     3              3           0
## 23       DYER     6              0           6
## 24    FAYETTE     3              1           2
## 25   FENTRESS     0              0           0
## 26   FRANKLIN     2              2           0
## 27     GIBSON     1              0           1
## 28      GILES     0              0           0
## 29   GRAINGER     0              0           0
## 30     GREENE     0              0           0
## 31     GRUNDY     0              0           0
## 32    HAMBLEN     0              0           0
## 33   HAMILTON     0              0           0
## 34    HANCOCK     0              0           0
## 35   HARDEMAN     0              0           0
## 36     HARDIN     1              0           1
## 37    HAWKINS     1              1           0
## 38    HAYWOOD     0              0           0
## 39  HENDERSON     1              1           0
## 40      HENRY     0              0           0
## 41    HICKMAN     0              0           0
## 42    HOUSTON     1              1           0
## 43  HUMPHREYS     0              0           0
## 44    JACKSON     0              0           0
## 45  JEFFERSON     0              0           0
## 46    JOHNSON     0              0           0
## 47       KNOX     0              0           0
## 48       LAKE     1              0           1
## 49 LAUDERDALE     0              0           0
## 50   LAWRENCE     1              1           0
## 51      LEWIS     1              1           0
## 52    LINCOLN     2              1           1
## 53     LOUDON     0              0           0
## 54      MACON     0              0           0
## 55    MADISON     0              0           0
## 56     MARION     3              3           0
## 57   MARSHALL     0              0           0
## 58      MAURY     0              0           0
## 59     MCMINN     0              0           0
## 60    MCNAIRY     0              0           0
## 61      MEIGS     0              0           0
## 62     MONROE     0              0           0
## 63 MONTGOMERY    13             13           0
## 64      MOORE     0              0           0
## 65     MORGAN     0              0           0
## 66      OBION     5              0           5
## 67    OVERTON     0              0           0
## 68      PERRY     0              0           0
## 69    PICKETT     0              0           0
## 70       POLK     0              0           0
## 71     PUTNAM     5              0           5
## 72       RHEA     0              0           0
## 73      ROANE     0              0           0
## 74  ROBERTSON     1              1           0
## 75 RUTHERFORD     0              0           0
## 76      SCOTT     0              0           0
## 77 SEQUATCHIE     0              0           0
## 78     SEVIER     0              0           0
## 79     SHELBY     2              0           2
## 80      SMITH     0              0           0
## 81    STEWART    32             32           0
## 82   SULLIVAN     1              1           0
## 83     SUMNER     1              0           1
## 84     TIPTON     0              0           0
## 85  TROUSDALE     0              0           0
## 86     UNICOI     0              0           0
## 87      UNION     1              1           0
## 88  VAN BUREN     0              0           0
## 89     WARREN     0              0           0
## 90 WASHINGTON     0              0           0
## 91      WAYNE     0              0           0
## 92    WEAKLEY     1              0           1
## 93      WHITE     0              0           0
## 94 WILLIAMSON     1              1           0
## 95     WILSON     0              0           0
totalsamples = list()

for (county in subset(County_Counts$County, County_Counts$Total > 0)) {
  print(county)
  cty <- subset(tn, subregion==tolower(county))
x <- cty[1]
y <- cty[2]
points <- subset(County_Counts$Total, County_Counts$County == toupper(county))

ctypoly1 <- sp::Polygon(cbind(x,y))
ctypoly1 <- sp::Polygons(list(ctypoly1), ID = "A")
samples <- spsample(ctypoly1,n=points,"random")
samples <- as.data.frame(samples@coords)

totalsamples[[county]] <- samples }
## [1] "BENTON"
## [1] "BLOUNT"
## [1] "BRADLEY"
## [1] "CHEATHAM"
## [1] "CLAIBORNE"
## [1] "DECATUR"
## [1] "DICKSON"
## [1] "DYER"
## [1] "FAYETTE"
## [1] "FRANKLIN"
## [1] "GIBSON"
## [1] "HARDIN"
## [1] "HAWKINS"
## [1] "HENDERSON"
## [1] "HOUSTON"
## [1] "LAKE"
## [1] "LAWRENCE"
## [1] "LEWIS"
## [1] "LINCOLN"
## [1] "MARION"
## [1] "MONTGOMERY"
## [1] "OBION"
## [1] "PUTNAM"
## [1] "ROBERTSON"
## [1] "SHELBY"
## [1] "STEWART"
## [1] "SULLIVAN"
## [1] "SUMNER"
## [1] "UNION"
## [1] "WEAKLEY"
## [1] "WILLIAMSON"
Obscured_Records = as.data.frame(do.call(rbind, totalsamples))
Obscured_Records = Obscured_Records[1:2]
Vertnet_Marbled$GUID <- paste(Vertnet_Marbled$institutioncode,":", Vertnet_Marbled$catalognumber, sep = "")

Specimen_List1 <- Vertnet_Marbled %>% group_by(county) %>% 
summarize(Specimen1 = paste(sort(unique(GUID)),collapse=", "))
names(Specimen_List1)[names(Specimen_List1) == 'county'] <- "COUNTY"
Specimen_List1$COUNTY <- toupper(Specimen_List1$COUNTY)
Specimen_List2 <- APSU_Marbled %>% group_by(COUNTY) %>% 
summarize(Specimen2 = paste(sort(unique(GUID)),collapse=", "))
Specimen_List2$COUNTY <- toupper(Specimen_List2$COUNTY)

Specimen_List <- merge(Specimen_List1, Specimen_List2, all = TRUE, by = "COUNTY")

Specimen_List$Specimens <- paste(Specimen_List$Specimen1, Specimen_List$Specimen2, sep = ", ")

Specimen_List$Specimen1 <- NULL
Specimen_List$Specimen2 <- NULL

Specimen_List$Specimens <- mgsub(Specimen_List$Specimens, "NA", "")

substr(Specimen_List$Specimens, 1, 2)  
##  [1] "US" "MV" "NC" ", " "YP" "KU" ", " "US" "US" ", " "YP" "UF" ", " ", " ", "
## [16] "CU" ", " ", " "UF" ", " ", " "KU" "NC" ", " "NC" ", " ", " "US" ", " "KU"
## [31] ", "
Specimen_List
##        COUNTY
## 1      BENTON
## 2      BLOUNT
## 3     BRADLEY
## 4    CHEATHAM
## 5   CLAIBORNE
## 6     DECATUR
## 7     DICKSON
## 8        DYER
## 9     FAYETTE
## 10   FRANKLIN
## 11     GIBSON
## 12     HARDIN
## 13    HAWKINS
## 14  HENDERSON
## 15    HOUSTON
## 16       LAKE
## 17   LAWRENCE
## 18      LEWIS
## 19    LINCOLN
## 20     MARION
## 21 MONTGOMERY
## 22      OBION
## 23     PUTNAM
## 24  ROBERTSON
## 25     SHELBY
## 26    STEWART
## 27   SULLIVAN
## 28     SUMNER
## 29      UNION
## 30    WEAKLEY
## 31 WILLIAMSON
##                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                           Specimens
## 1                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                      USNM:45958, 
## 2                                                                                                                                                                                                                                                                                                                                                                                                                                                                MVZ:MVZ:Herp:40946, USNM:540940, USNM:540941, USNM:540942, USNM:540943, USNM:540944, USNM:540945, 
## 3                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                      NCSM:61623, 
## 4                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               , APSU:Herp:03532, APSU:Herp:04007, APSU:Herp:19270
## 5                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                             YPM:YPM HERA 013670, 
## 6                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                       KU:2639, KU:2640, KU:2641, 
## 7                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               , APSU:Herp:02776, APSU:Herp:03159, APSU:Herp:20066
## 8                                                                                                                                                                                                                                                                                                                                                                                                                                                                                          USNM:28394, USNM:28395, USNM:28396, USNM:28397, USNM:28398, USNM:28399, 
## 9                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                           USNM:99130, USNM:99131, APSU:Herp:18050
## 10                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               , APSU:Herp:03257, APSU:Herp:06024
## 11                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            YPM:YPM HERA 020516, 
## 12                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                      UF:171994, 
## 13                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                , APSU:Herp:18168
## 14                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                , APSU:Herp:19197
## 15                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                , APSU:Herp:06056
## 16                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 CUMV:A-0003507, 
## 17                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                , APSU:Herp:17622
## 18                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                , APSU:Herp:17623
## 19                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                       UF:171982, APSU:Herp:19974
## 20                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                              , APSU:Herp:17627, APSU:Herp:17628, APSU:Herp:17644
## 21                                                                                                                                                                                                                                                                                                                                    , APSU:Herp:02044, APSU:Herp:03026, APSU:Herp:03311, APSU:Herp:04293, APSU:Herp:05199, APSU:Herp:05200, APSU:Herp:05210, APSU:Herp:05305, APSU:Herp:05313, APSU:Herp:05316, APSU:Herp:05356, APSU:Herp:05362, APSU:Herp:05400
## 22                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        KU:155854, KU:155855, KU:155856, KU:155857, USNM:103100, 
## 23                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                          NCSM:8683, NCSM:8684, NCSM:8685, NCSM:8686, NCSM:8687, 
## 24                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                , APSU:Herp:02925
## 25                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            NCSM:235, USNM:6449, 
## 26 , APSU:Herp:00920, APSU:Herp:02801, APSU:Herp:03758, APSU:Herp:04655, APSU:Herp:04895, APSU:Herp:04984, APSU:Herp:04985, APSU:Herp:05005, APSU:Herp:05043, APSU:Herp:05051, APSU:Herp:05057, APSU:Herp:05062, APSU:Herp:05211, APSU:Herp:05388, APSU:Herp:05417, APSU:Herp:05513, APSU:Herp:05519, APSU:Herp:05528, APSU:Herp:05668, APSU:Herp:05697, APSU:Herp:05732, APSU:Herp:05735, APSU:Herp:05736, APSU:Herp:05749, APSU:Herp:05806, APSU:Herp:05807, APSU:Herp:05893, APSU:Herp:05894, APSU:Herp:05902, APSU:Herp:05997, APSU:Herp:05998, APSU:Herp:18045
## 27                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                , APSU:Herp:18170
## 28                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                      USNM:3933, 
## 29                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                , APSU:Herp:17709
## 30                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                       KU:98590, 
## 31                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                , APSU:Herp:19656
ggplot() +
  geom_polygon(data = state, aes(x=long, y = lat, group = group),
                        fill = "white", color="black") + 
           geom_polygon(data = tn, aes(x=long, y = lat, group = group),
                        fill = "gray", color="black") +
  geom_point(data = Obscured_Records, aes(x = Obscured_Records$x, y = Obscured_Records$y), color = "black", size = 2) +
  coord_fixed(xlim = c(-90.5, -81.5),  ylim = c(34.75, 36.75), ratio = 1.2) + 
  xlab("Longitude") + ylab("Latitude") + ggtitle("APSU, Montgomery Co., TN")
## Warning: Use of `Obscured_Records$x` is discouraged. Use `x` instead.
## Warning: Use of `Obscured_Records$y` is discouraged. Use `y` instead.

ggplot() +
  geom_polygon(data = state, aes(x=long, y = lat, group = group),
                        fill = "white", color="black") + 
           geom_polygon(data = tn, aes(x=long, y = lat, group = group),
                        fill = "gray", color="black") + 
  geom_point(data = Inat_Marbled, aes(Inat_Marbled$longitude, Inat_Marbled$latitude), color = "#1f7d1f", size = 2) +
  geom_point(data = GBif_Marbled, aes(GBif_Marbled$decimalLongitude, GBif_Marbled$decimalLatitude), color = "dark blue", size = 2) +    
  geom_point(data = Vertnet_Marbled, aes(Vertnet_Marbled$decimallongitude, Vertnet_Marbled$decimallatitude), color = "dark orange", size = 2) +
  geom_point(data = APSU_Marbled, aes(x = APSU_Marbled$DEC_LONG, y = APSU_Marbled$DEC_LAT), color = "maroon", size = 2) +
  coord_fixed(xlim = c(-90.5, -81.5),  ylim = c(34.75, 36.75), ratio = 1.2) + 
  xlab("Longitude") + ylab("Latitude") + ggtitle("APSU, Montgomery Co., TN")
## Warning: Use of `Inat_Marbled$longitude` is discouraged. Use `longitude`
## instead.
## Warning: Use of `Inat_Marbled$latitude` is discouraged. Use `latitude` instead.
## Warning: Use of `GBif_Marbled$decimalLongitude` is discouraged. Use
## `decimalLongitude` instead.
## Warning: Use of `GBif_Marbled$decimalLatitude` is discouraged. Use
## `decimalLatitude` instead.
## Warning: Use of `Vertnet_Marbled$decimallongitude` is discouraged. Use
## `decimallongitude` instead.
## Warning: Use of `Vertnet_Marbled$decimallatitude` is discouraged. Use
## `decimallatitude` instead.
## Warning: Use of `APSU_Marbled$DEC_LONG` is discouraged. Use `DEC_LONG` instead.
## Warning: Use of `APSU_Marbled$DEC_LAT` is discouraged. Use `DEC_LAT` instead.

label <- toupper(c(County_Counts$County))

leaflet(APSU_Marbled) %>% 
  addTiles(group = "Base Map") %>%
  addWMSTiles("https://basemap.nationalmap.gov/arcgis/services/USGSTopo/MapServer/WmsServer", layers = "0", group = "Terrain Map") %>% 
  addPolygons(data= tn_SP, fillColor = "white", color = "black", weight = 1, label = label) %>%
  addCircleMarkers(data = Vertnet_Marbled, lng=Vertnet_Marbled$decimallongitude, lat=Vertnet_Marbled$decimallatitude, color = "orange", radius = .5, label = as.Date(Vertnet_Marbled$eventdate), group = "Vertnet Records") %>%
  addCircleMarkers(data = GBif_Marbled, lng=GBif_Marbled$decimalLongitude, lat=GBif_Marbled$decimalLatitude, color = "#003153", radius = .5, label = as.Date(GBif_Marbled$eventDate), group = "GBif Records") %>%
  addCircleMarkers(data = Inat_Marbled, lng=Inat_Marbled$longitude, lat=Inat_Marbled$latitude, color = "green", radius = .5, label = as.Date(Inat_Marbled$datetime),group = "INaturalist Records") %>%
  addCircleMarkers(lng=APSU_Marbled$DEC_LONG, lat=APSU_Marbled$DEC_LAT, color = "maroon", radius = .5, label = as.Date(APSU_Marbled$VERBATIM_DATE), group = "APSU Records") %>%
  addLayersControl(
    baseGroups = c("Base Map", "Terrain Map"), overlayGroups = c("APSU Records", "Vertnet Records", "INaturalist Records", "GBif Records"),
    options = layersControlOptions(collapsed = FALSE)) %>%
    addScaleBar() %>%
    addMiniMap(zoomLevelOffset = -4)
APSU_Marbled <- read.csv("https://raw.githubusercontent.com/CJHughes926/Herpmapping_Map_Assignment2/main/APSU_Ambystoma_opacum_TN.csv")
  
Vertnet_Marbled <- rvertnet::searchbyterm(scientificname = "Ambystoma opacum", limit = 10000)
## Processing request...
## 
## Last Query URL: "http://api.vertnet-portal.appspot.com/api/search?q={"q":"scientificname:Ambystoma opacum","l":1000,"c":"False:CvUFCtwDCqwD9wAAAAL_____jIGJmo2LkZqL0o-QjYuek96WkZuah9LNz87M0s_H0s_H_wAA_3RtoKCZi4ygoP8AAP9dno-PmpGYlpGa_wAA_3N0bZaRm5qH_wAA_12biJz_AAD_c3Rtm5CcoJab_wAA_12ekpGX0JeajY-ai5CTkJiG0IqNkdKcnouek5CY0p6SkZfSl5qNj5qLkJOQmIbSntLMyMnLyP8AAP9zf56SkZfQl5qNj5qLkJOQmIbQio2R0pyei56TkJjSnpKRl9KXmo2PmouQk5CYhtKe0szIycvI_wAA__8A_v_-jIGJmo2LkZqL0o-QjYuek96WkZuah9LNz87M0s_H0s_H_wB0baCgmYuMoKD_AF2ej4-akZiWkZr_AHN0bZaRm5qH_wBdm4ic_wBzdG2bkJyglpv_AF2ekpGX0JeajY-ai5CTkJiG0IqNkdKcnouek5CY0p6SkZfSl5qNj5qLkJOQmIbSntLMyMnLyP8Ac3-ekpGX0JeajY-ai5CTkJiG0IqNkdKcnouek5CY0p6SkZfSl5qNj5qLkJOQmIbSntLMyMnLyP8A__4QqEYh214v0Zsk1Mk5AAAAAP3___9IuQNQAFoLCeJaak7PsEpHEANg2YjQhwYSDURvY3VtZW50SW5kZXga4gEoQU5EIChJUyAiY3VzdG9tZXJfbmFtZSIgImFwcGVuZ2luZSIpIChJUyAiZ3JvdXBfbmFtZSIgInN-dmVydG5ldC1wb3J0YWwiKSAoSVMgIm5hbWVzcGFjZSIgImluZGV4LTIwMTMtMDgtMDgiKSAoSVMgImluZGV4X25hbWUiICJkd2MiKSAoQU5EIChRVCAiQW1ieXN0b21hIiAicnRleHRfc2NpZW50aWZpY25hbWUiKSAoT1IgKFFUICJvcGFjdW0iKSAoSVMgIl9fZ2F0b21fXyIgIm9wYWN1bSIpKSkpOhkKDChOIG9yZGVyX2lkKRABGQAAAAAAAPD_SgUIAEDoBw"}"
## 
## Matching records: 9456 returned, 9456 available
Vertnet_Marbled <- Vertnet_Marbled$data

Inat_Marbled <- rinat::get_inat_obs(taxon_name = "Ambystoma opacum", maxresults = 10000, quality = "research", geo =  TRUE)

GBif_Marbled <- rgbif::occ_data(scientificName = "Ambystoma opacum",
                 hasCoordinate = TRUE, limit = 10000,
                 decimalLongitude = "-100, -70", 
                 decimalLatitude = "29, 46.547505", )
GBif_Marbled <- GBif_Marbled$data
APSU_Marbled <- read.csv("https://raw.githubusercontent.com/CJHughes926/Herpmapping_Map_Assignment2/main/APSU_Ambystoma_opacum_TN.csv")
  
Vertnet_Marbled <- rvertnet::searchbyterm(scientificname = "Ambystoma opacum", limit = 10000)
## Processing request...
## 
## Last Query URL: "http://api.vertnet-portal.appspot.com/api/search?q={"q":"scientificname:Ambystoma opacum","l":1000,"c":"False:CvQFCtsDCqwD9wAAAAL_____jIGJmo2LkZqL0o-QjYuek96WkZuah9LNz87M0s_H0s_H_wAA_3RtoKCZi4ygoP8AAP9dno-PmpGYlpGa_wAA_3N0bZaRm5qH_wAA_12biJz_AAD_c3Rtm5CcoJab_wAA_12ekpGX0JeajY-ai5CTkJiG0IqNkdKcnouek5CY0p6SkZfSl5qNj5qLkJOQmIbSntLMyMnLyP8AAP9zf56SkZfQl5qNj5qLkJOQmIbQio2R0pyei56TkJjSnpKRl9KXmo2PmouQk5CYhtKe0szIycvI_wAA__8A_v_-jIGJmo2LkZqL0o-QjYuek96WkZuah9LNz87M0s_H0s_H_wB0baCgmYuMoKD_AF2ej4-akZiWkZr_AHN0bZaRm5qH_wBdm4ic_wBzdG2bkJyglpv_AF2ekpGX0JeajY-ai5CTkJiG0IqNkdKcnouek5CY0p6SkZfSl5qNj5qLkJOQmIbSntLMyMnLyP8Ac3-ekpGX0JeajY-ai5CTkJiG0IqNkdKcnouek5CY0p6SkZfSl5qNj5qLkJOQmIbSntLMyMnLyP8A__4QqEYh214v0Zsk1Mk5AAAAAP3___9IuQNQAFoLCeJaak7PsEpHEANgw9yDEBINRG9jdW1lbnRJbmRleBriAShBTkQgKElTICJjdXN0b21lcl9uYW1lIiAiYXBwZW5naW5lIikgKElTICJncm91cF9uYW1lIiAic352ZXJ0bmV0LXBvcnRhbCIpIChJUyAibmFtZXNwYWNlIiAiaW5kZXgtMjAxMy0wOC0wOCIpIChJUyAiaW5kZXhfbmFtZSIgImR3YyIpIChBTkQgKFFUICJBbWJ5c3RvbWEiICJydGV4dF9zY2llbnRpZmljbmFtZSIpIChPUiAoUVQgIm9wYWN1bSIpIChJUyAiX19nYXRvbV9fIiAib3BhY3VtIikpKSk6GQoMKE4gb3JkZXJfaWQpEAEZAAAAAAAA8P9KBQgAQOgH"}"
## 
## Matching records: 9456 returned, 9456 available
Vertnet_Marbled <- Vertnet_Marbled$data

Inat_Marbled <- rinat::get_inat_obs(taxon_name = "Ambystoma opacum", maxresults = 1000, quality = "research", geo =  TRUE)

GBif_Marbled <- rgbif::occ_data(scientificName = "Ambystoma opacum",
                 hasCoordinate = TRUE, limit = 5000,
                 decimalLongitude = "-90.5, -81.5", 
                 decimalLatitude = "34, 37", )

GBif_Marbled <- GBif_Marbled$data
APSU_Marbled <- as.data.frame(APSU_Marbled)
APSU_Marbled <- subset(APSU_Marbled, is.na(APSU_Marbled$DEC_LAT) == FALSE)
APSU_Marbled$COUNTY <- mgsub(APSU_Marbled$COUNTY, " County", "")

Vertnet_Marbled <- as.data.frame(Vertnet_Marbled)
Vertnet_Marbled <- subset(Vertnet_Marbled, is.na(Vertnet_Marbled$decimallatitude) == FALSE)
Vertnet_Marbled <- subset(Vertnet_Marbled, is.na(Vertnet_Marbled$decimallongitude) == FALSE)
Vertnet_Marbled$county <- mgsub(Vertnet_Marbled$county, " County", "")


Vertnet_Marbled$decimallatitude <- as.numeric(Vertnet_Marbled$decimallatitude)
Vertnet_Marbled$decimallongitude <- as.numeric(Vertnet_Marbled$decimallongitude)

Inat_Marbled <- as.data.frame(Inat_Marbled, )
Inat_Marbled <- subset(Inat_Marbled, Inat_Marbled$coordinates_obscured == "false")
Inat_Marbled <- subset(Inat_Marbled, is.na(Inat_Marbled$latitude) == FALSE)

GBif_Marbled <- as.data.frame(GBif_Marbled)
GBif_Marbled <- subset(GBif_Marbled, is.na(GBif_Marbled$decimalLatitude) == FALSE)
Vertnet_Marbled <- as.data.frame(Vertnet_Marbled)
Vertnet_Marbled <- subset(Vertnet_Marbled, is.na(Vertnet_Marbled$decimallatitude) == FALSE)
Vertnet_Marbled <- subset(Vertnet_Marbled, is.na(Vertnet_Marbled$decimallongitude) == FALSE)
Vertnet_Marbled <- subset(Vertnet_Marbled, Vertnet_Marbled$country == "USA" | Vertnet_Marbled$country == "United States")
Vertnet_Marbled <- subset(Vertnet_Marbled, Vertnet_Marbled$decimallongitude < min(Inat_Marbled$longitude) |  Vertnet_Marbled$decimallatitude > max(Inat_Marbled$latitude))
Vertnet_Marbled$county <- mgsub(Vertnet_Marbled$county, " County", "")
Vertnet_Marbled <- subset(Vertnet_Marbled, as.Date(Vertnet_Marbled$eventdate) != "1973-09-30")


Vertnet_Marbled$decimallatitude <- as.numeric(Vertnet_Marbled$decimallatitude)
Vertnet_Marbled$decimallongitude <- as.numeric(Vertnet_Marbled$decimallongitude)

Inat_Marbled <- as.data.frame(Inat_Marbled)
Inat_Marbled <- subset(Inat_Marbled, Inat_Marbled$coordinates_obscured == "false")
Inat_Marbled <- subset(Inat_Marbled, is.na(Inat_Marbled$latitude) == FALSE)
new_df <- APSU_Marbled[,c("DEC_LAT","DEC_LONG")]
new_df2 <- Vertnet_Marbled[,c("decimallatitude", "decimallongitude")]
new_df3 <- Inat_Marbled[,c("latitude","longitude")]
new_df4 <- GBif_Marbled[,c("decimalLatitude","decimalLongitude")]

names(new_df) <- c("x","y")
names(new_df2) <- c("x","y")
names(new_df3) <- c("x","y")
names(new_df4) <- c("x","y")

lot_points <- rbind(new_df, new_df2, new_df3, new_df4)
lot_points <- subset(lot_points, is.na(lot_points$x) == FALSE)
lot_points$x <- as.numeric(lot_points$x)
lot_points$y <- as.numeric(lot_points$y)

lot_points <- subset(lot_points, lot_points$y < 0)
bioclim <- getData(name = "worldclim", res = 2.5, var = "bio", path = "./")
names(bioclim) <- c("Ann Mean Temp","Mean Diurnal Range","Isothermality","Temperature Seasonality",
                    "Max Temp Warmest Mo","Min Temp Coldest Mo","Ann Temp Range","Mean Temp Wettest Qtr",
                    "Mean Temp Driest Qtr","Mean Temp Warmest Qtr","Mean Temp Coldest Qtr","Annual Precip",
                    "Precip Wettest Mo","Precip Driest Mo","Precip Seasonality","Precip Wettest Qtr",
                    "Precip Driest Qtr","Precip Warmest Qtr","Precip Coldest Qtr")
bio_extent <- extent(x = c(
  min(lot_points$y),
  max(lot_points$y),
  min(lot_points$x),
  max(lot_points$x)))

bioclim_extent <- crop(x = bioclim, y = bio_extent)
bioclim_model <- bioclim(x = bioclim_extent, p = cbind(lot_points$y,lot_points$x))
presence_model <- dismo::predict(object = bioclim_model, 
                                 x = bioclim_extent, 
                                 ext = bio_extent)
gplot(presence_model) + 
  geom_polygon(data = state, aes(x=long, y = lat, group = group),
                        fill = "white", color="black") + 
  geom_raster(aes(fill=value)) +
  geom_polygon(data = tn, aes(x=long, y = lat, group = group),
                        fill = NA, color="black") + 
  geom_point(data = Inat_Marbled, aes(Inat_Marbled$longitude, Inat_Marbled$latitude), color = "#1f7d1f", size = 2) +
  geom_point(data = GBif_Marbled, aes(GBif_Marbled$decimalLongitude, GBif_Marbled$decimalLatitude), color = "dark blue", size = 2) +    
  geom_point(data = Vertnet_Marbled, aes(Vertnet_Marbled$decimallongitude, Vertnet_Marbled$decimallatitude), color = "dark orange", size = 2) +
  geom_point(data = APSU_Marbled, aes(x = APSU_Marbled$DEC_LONG, y = APSU_Marbled$DEC_LAT), color = "maroon", size = 2) +
  scale_fill_gradientn(colours=c("brown","yellow","darkgreen"), "Probability") +
  coord_fixed(xlim = c(-90.5, -81.5),  ylim = c(34.75, 36.75)) + 
  xlab("Longitude") + ylab("Latitude") + ggtitle("Probability of PILO Occurrence") + 
  theme_bw() + theme(plot.title = element_text(hjust = 0.5)) + theme(legend.position = "right") +
  theme(panel.grid.major = element_blank(), 
        panel.grid.minor = element_blank(),
        panel.background = element_rect(fill = "lightblue"))
## Warning: Use of `Inat_Marbled$longitude` is discouraged. Use `longitude`
## instead.
## Warning: Use of `Inat_Marbled$latitude` is discouraged. Use `latitude` instead.
## Warning: Use of `GBif_Marbled$decimalLongitude` is discouraged. Use
## `decimalLongitude` instead.
## Warning: Use of `GBif_Marbled$decimalLatitude` is discouraged. Use
## `decimalLatitude` instead.
## Warning: Use of `Vertnet_Marbled$decimallongitude` is discouraged. Use
## `decimallongitude` instead.
## Warning: Use of `Vertnet_Marbled$decimallatitude` is discouraged. Use
## `decimallatitude` instead.
## Warning: Use of `APSU_Marbled$DEC_LONG` is discouraged. Use `DEC_LONG` instead.
## Warning: Use of `APSU_Marbled$DEC_LAT` is discouraged. Use `DEC_LAT` instead.

label <- toupper(c(County_Counts$County))
colors <- c("brown","yellow","darkgreen")

leaflet(APSU_Marbled) %>% 
  addTiles(group = "Base Map") %>%
  addWMSTiles("https://basemap.nationalmap.gov/arcgis/services/USGSTopo/MapServer/WmsServer", layers = "0", group = "Terrain Map") %>% 
  addRasterImage(presence_model, colors = colors, opacity = 0.8) %>%
  addPolygons(data= tn_SP, fillColor = "white", color = "black", weight = 1, label = label) %>%
  addCircleMarkers(data = Vertnet_Marbled, lng=Vertnet_Marbled$decimallongitude, lat=Vertnet_Marbled$decimallatitude, color = "orange", radius = .5, label = as.Date(Vertnet_Marbled$eventdate), group = "Vertnet Records") %>%
  addCircleMarkers(data = GBif_Marbled, lng=GBif_Marbled$decimalLongitude, lat=GBif_Marbled$decimalLatitude, color = "#003153", radius = .5, label = as.Date(GBif_Marbled$eventDate), group = "GBif Records") %>%
  addCircleMarkers(data = Inat_Marbled, lng=Inat_Marbled$longitude, lat=Inat_Marbled$latitude, color = "green", radius = .5, label = as.Date(Inat_Marbled$datetime),group = "INaturalist Records") %>%
  addCircleMarkers(lng=APSU_Marbled$DEC_LONG, lat=APSU_Marbled$DEC_LAT, color = "maroon", radius = .5, label = as.Date(APSU_Marbled$VERBATIM_DATE), group = "APSU Records") %>%
  addLayersControl(
    baseGroups = c("Base Map", "Terrain Map"), overlayGroups = c("APSU Records", "Vertnet Records", "INaturalist Records", "GBif Records"),
    options = layersControlOptions(collapsed = FALSE)) %>%
    addScaleBar() %>%
    addMiniMap(zoomLevelOffset = -4)
## Warning in showSRID(uprojargs, format = "PROJ", multiline = "NO", prefer_proj =
## prefer_proj): Discarded ellps WGS 84 in Proj4 definition: +proj=merc +a=6378137
## +b=6378137 +lat_ts=0 +lon_0=0 +x_0=0 +y_0=0 +k=1 +units=m +nadgrids=@null
## +wktext +no_defs +type=crs
## Warning in showSRID(uprojargs, format = "PROJ", multiline = "NO", prefer_proj =
## prefer_proj): Discarded datum World Geodetic System 1984 in Proj4 definition
## Warning in showSRID(uprojargs, format = "PROJ", multiline = "NO", prefer_proj =
## prefer_proj): Discarded ellps WGS 84 in Proj4 definition: +proj=merc +a=6378137
## +b=6378137 +lat_ts=0 +lon_0=0 +x_0=0 +y_0=0 +k=1 +units=m +nadgrids=@null
## +wktext +no_defs +type=crs
## Warning in showSRID(uprojargs, format = "PROJ", multiline = "NO", prefer_proj =
## prefer_proj): Discarded datum World Geodetic System 1984 in Proj4 definition